//
//  BindBankViewController.m
//  WBBaseProject
//
//  Created by xiaozhi on 2018/3/30.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "BindBankViewController.h"
#import "Infoinput.h"
#import "SetTradPwdViewController.h"

#import "ValuePickerView.h"

@interface BindBankViewController (){
    dispatch_source_t _timer;
    
    NSString *bankName;
    NSInteger isdefaultCard;
    
    NSArray *bankListArr;
    NSMutableArray *bankNameList;
}

@property(nonatomic,strong)UIScrollView* BGview;

@property(nonatomic,strong)ValuePickerView *bankList;
@property(nonatomic,strong)Infoinput* name;
@property(nonatomic,strong)Infoinput* IDnum;
@property(nonatomic,strong)Infoinput* chooseBank;
@property(nonatomic,strong)Infoinput* bankNum;
@property(nonatomic,strong)Infoinput* phoneNum;
@property(nonatomic,strong)Infoinput* code;

/** <#注释#> */
@property (nonatomic,strong) UIButton *getCodeBtn;

@end

#define rowH 50
@implementation BindBankViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WB_RGBColor(240, 240, 240);
    self.title=@"绑定银行卡";
    
    [self.view addSubview:self.BGview];
    
    [self.BGview addSubview:self.name];
    [self.BGview addSubview:self.IDnum];
    [self.BGview addSubview:self.chooseBank];
    [self.BGview addSubview:self.bankNum];
    [self.BGview addSubview:self.phoneNum];
    [self.BGview addSubview:self.code];
    
    self.BGview.contentSize = CGSizeMake(kScreenWidth, kScreenHeight+1);
    [self getData];
}
- (void)getData
{
    [XYHttpRequest dataRequestWithSupportBankCardListType:0 success:^(id responseObj) {
        
        if (Success) {
            bankListArr = responseObj[@"data"];
            bankNameList = [NSMutableArray array];
            
            for (NSDictionary* dic in bankListArr) {
                
                [bankNameList addObject:dic[@"NAME"]];
            }
            
            self.bankList.dataSource = bankNameList;
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark - 懒加载  Lazy Load
- (UIScrollView *)BGview
{
    if (!_BGview) {
        _BGview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    }
    return _BGview;
}

- (Infoinput *)name
{
    if (!_name) {
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 30)];
        la.font = WB_FontSize(12);
        la.textColor = WB_RGBColor(128, 128, 128);
        la.text = @"身份信息";
        [self.BGview addSubview:la];
        
        _name = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        _name.frame = CGRectMake(0, la.mm_bottom, kScreenWidth, rowH);
        _name.inputTf.placeholder = @"输入姓名";
        
    }
    return _name;
}
- (Infoinput *)IDnum
{
    if (!_IDnum) {
        _IDnum = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        _IDnum.frame = CGRectMake(0, self.name.mm_bottom, kScreenWidth, rowH);
        _IDnum.inputTf.placeholder = @"输入身份证号";
    }
    return _IDnum;
}

- (Infoinput *)chooseBank
{
    if (!_chooseBank) {
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(15, self.IDnum.mm_bottom, 100, 30)];
        la.font = WB_FontSize(12);
        la.textColor = WB_RGBColor(128, 128, 128);
        la.text = @"银行卡信息";
        [self.BGview addSubview:la];
        
        _chooseBank = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        _chooseBank.chooseV.hidden = NO;
        _chooseBank.frame = CGRectMake(0, la.mm_bottom, kScreenWidth, rowH);
        
#define mark 选择银行
        WB_WEAKSELF;
        _chooseBank.click = ^{
            
            [weakSelf.view endEditing:YES];
            [weakSelf.BGview addSubview:weakSelf.bankList];
            
            [weakSelf.bankList show];
            
        };
    }
    return _chooseBank;
}
- (ValuePickerView *)bankList
{
    if (!_bankList) {
        
        _bankList = [[ValuePickerView alloc]init];
     
        WB_WEAKSELF;
        __block NSString *name;
        _bankList.valueDidSelect = ^(NSInteger row) {

            NSLog(@"%ld",(long)row);

            bankName = bankListArr[row][@"NAME"];
            name = bankName;
            [weakSelf.chooseBank.chooseBtn setTitle:name forState:UIControlStateNormal];
            [weakSelf.chooseBank.chooseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        };
        
    }
    return _bankList;
}
- (Infoinput *)bankNum
{
    if (!_bankNum) {
        _bankNum = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        
        _bankNum.frame = CGRectMake(0, self.chooseBank.mm_bottom, kScreenWidth, rowH);
        
        _bankNum.inputTf.placeholder = @"输入银行卡号";
    }
    return _bankNum;
}
- (Infoinput *)phoneNum
{
    if (!_phoneNum) {
        _phoneNum= [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        
        _phoneNum.frame = CGRectMake(0, self.bankNum.mm_bottom+15, kScreenWidth, rowH);
        
        _phoneNum.inputTf.placeholder = @"输入银行预留手机号";
    }
    return _phoneNum;
}
- (Infoinput *)code
{
    if (!_code) {
        _code = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Infoinput class])  owner:self options:nil].firstObject;
        
        _code.frame = CGRectMake(0, self.phoneNum.mm_bottom, kScreenWidth, rowH);
        
        _code.inputTf.placeholder = @"输入银手机验证码";
        
        UIButton *getCode = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-100, 0, 85, rowH)];
        [getCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        [getCode setTitleColor:WB_RGBColor(52, 124, 255) forState:UIControlStateNormal];
        getCode.titleLabel.font = _code.inputTf.font;
        _getCodeBtn = getCode;
        [getCode addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
        [_code addSubview:getCode];
        
        UIButton *add = [[UIButton alloc]init];
        [add addTarget:self action:@selector(setBank:) forControlEvents:UIControlEventTouchUpInside];
        [add setBackgroundImage:[UIImage imageNamed:@"单选-"] forState:UIControlStateNormal];
        [add setBackgroundImage:[UIImage imageNamed:@"对号"] forState:UIControlStateSelected];
        add.frame = CGRectMake(15, _code.mm_bottom+10, 20, 20);
        [self.BGview addSubview:add];
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(add.mm_right+10, _code.mm_bottom+10, 100, 20)];
        la.font = WB_FontSize(13);
        la.textColor = WB_RGBColor(128, 128, 128);
        la.text = @"设为默认银行卡";
        [self.BGview addSubview:la];
        
        UIButton *next = [Factory customButtonWithTitle:@"下一步"];
        next.frame = CGRectMake(35, la.mm_bottom + 50, kScreenWidth - 70, 60);
        [next addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.BGview addSubview:next];
    }
    return _code;
}

- (void)getCode
{
    if (WB_IsNilString(_phoneNum.inputTf.text)) {
        [NSObject wb_showAlertWithTitle:@"手机号码不能为空"];
        return;
    }else if (![Factory isPhoneNumber:_phoneNum.inputTf.text]){
        [NSObject wb_showAlertWithTitle:@"请输入正确的手机号码"];
        return;
    }
    [XYHttpRequest dataRequestGetMessageWithPhone:_phoneNum.inputTf.text codeLength:6 success:^(id responseObj) {
        [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
        if (Success) {
            WB_DISPATCH_MAIN_THREAD(^{
                _timer = [Factory startTimeWithBtn:_getCodeBtn];  
            });
        }
    } failure:^(NSError *error) {
    }];
    
}
- (void)nextClick:(UIButton *)btn
{
    if (WB_IsNilString(_name.inputTf.text)) {
        
        [NSObject wb_showAlertWithTitle:@"姓名不能为空"];
        return;
    }else if (WB_IsNilString(_IDnum.inputTf.text)) {
        
        [NSObject wb_showAlertWithTitle:@"身份证号不能为空"];
        return;
    }else if (WB_IsNilString(bankName)) {
        
        [NSObject wb_showAlertWithTitle:@"银行名称不能为空"];
        return;
    }else if (WB_IsNilString(_bankNum.inputTf.text)) {
        
        [NSObject wb_showAlertWithTitle:@"银行卡号不能为空"];
        return;
    }else if (WB_IsNilString(_code.inputTf.text)) {
        
        [NSObject wb_showAlertWithTitle:@"验证码不能为空"];
        return;
    }
    
    WB_WeakSelf(self)
    [XYHttpRequest dataRequestWithCertificationBindCardCustName:_name.inputTf.text custIc:_IDnum.inputTf.text bankName:@"浦发银行" custBindCard:_bankNum.inputTf.text custBindCardMobile:_phoneNum.inputTf.text code:_code.inputTf.text defaultCard:@(isdefaultCard) success:^(id responseObj) {
        if (Success) {
            if ([[[NSUserDefaults standardUserDefaults] objectForKey:UserInfo][@"tradePWFlag"] isEqualToString:@"0"]) {
                [NSObject wb_showAlertWithTitle:@"绑定成功!" duration:1];
                WB_DISPATCH_MAIN_Delay_THREAD(1, ^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                })
            }else{
                SetTradPwdViewController *vc = [[SetTradPwdViewController alloc] init];
                [weakself.navigationController pushViewController:vc animated:YES];
            }
        }else{
            [NSObject wb_showAlertWithTitle:responseObj[@"info"] duration:1];
        }
    } failure:^(NSError *error) {
    }];
}

//设为默认银行卡
- (void)setBank:(UIButton *)btn
{
    btn.selected = !btn.selected;
    isdefaultCard = @(btn.selected).integerValue;
}


@end
